package com.studio49.exmple.core.component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * 
 * @author chensj
 * @date 2019年2月16日  下午2:39:05
 * @version 1.0
 */
@Component
public class RedisComponent {
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate; 
	
	 /**
     * 指定时间类型---秒
     * @param key
     * @return
     */
    public long getExpireTime(String key){
        long time = redisTemplate.getExpire(key, TimeUnit.SECONDS);
        return time;
    }
	
	//**************************key-val*****************************************************
	/**
	 * save key-value
	 * @param key			key
	 * @param val			val
	 * @param expire		过期时间，单位：秒
	 * @return
	 */
	public void saveData(String key, Object val, int expire) {
		ValueOperations<String, Object> operations = redisTemplate.opsForValue(); 
		// 插入缓存 
		operations.set(key, val, expire, TimeUnit.SECONDS);
	}
	
	/**
	 * get key-val
	 * @param key
	 * @return
	 */
	public Object getData(String key) {
		ValueOperations<String, Object> operations = redisTemplate.opsForValue(); 
		// 插入缓存 
		Object object = operations.get(key);
		return object;
	}
	
	/**
	 * 删除key-val
	 * @param key
	 */
	public void deleteData(String key) {
		ValueOperations<String, Object> operations = redisTemplate.opsForValue(); 
		// 删除
		operations.getOperations().delete(key);
	}
	
	
	//**************************key-hash*****************************************************
	
	/**
	 * save key-hash
	 * @param key			key
	 * @param map			map
	 */
	public void insertMap(String key, Map<String, Object> map) { 
		HashOperations<String, String, Object> operations = redisTemplate.opsForHash(); 
		operations.putAll(key, map);
	}

	/**
	 * get key-hash
	 * @param key
	 * @return
	 */
	public Object getHash(String key, String hashKey) {
		HashOperations<String, String, Object> operations = redisTemplate.opsForHash(); 
		Object object = operations.get(key, hashKey);
		return object;
	}
	
	/**
	 * delete key-map
	 * @param key
	 * @param map
	 */
	public void deleteMap(String key) { 
		HashOperations<String, String, Object> operations = redisTemplate.opsForHash(); 
		operations.getOperations().delete(key);
	}

	//**************************key-list*****************************************************
	//增删改查list 
	/**
	 * save key-list
	 * @param key
	 * @param list
	 */
	public void insertList(String key, List<Object> list) { 
		ListOperations<String, Object> operations = redisTemplate.opsForList(); 
		operations.leftPushAll(key, list); 
	}

	/**
	 * get key-hash
	 * @param key
	 * @return
	 */
	public List<Object> getList(String key, int start, int end) {
		ListOperations<String, Object> operations = redisTemplate.opsForList();
		// 插入缓存 
		List<Object> range = operations.range(key, start, end);
		return range;
	}
	/**
	 * delete key-list
	 * @param key
	 * @param map
	 */
	public void deleteList(String key) { 
		ListOperations<String, Object> operations = redisTemplate.opsForList();
		operations.getOperations().delete(key);
	}
	
}
